<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="类型 " prop="type"><!-- (协助招生，教师培训，组织比赛) -->
      <!-- <el-input v-model="dataForm.type" placeholder="类型 (协助招生，教师培训，组织比赛)"></el-input> -->
      <el-select v-model="dataForm.type" placeholder="请选择">
        <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value"> </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="图片地址" prop="imgUrl">
      <!-- <el-input v-model="dataForm.imgUrl" placeholder="图片地址"></el-input> -->
      <el-upload
        class="avatar-uploader"
        :action="url"
        :show-file-list="false"
        :on-success="handleAvatarSuccess"
        :before-upload="beforeAvatarUpload">
        <img v-if="imageUrl" :src="imageUrl" class="avatar">
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      </el-upload>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        imageUrl: '',
        url: '',
        options: [{
          value: 0,
          label: '协助招生'
        }, {
          value: 1,
          label: '教师培训'
        }, {
          value: 2,
          label: '组织比赛'
        }],
        visible: false,
        dataForm: {
          brandActivityId: 0,
          type: '',
          imgUrl: ''
        },
        dataRule: {
          type: [
            { required: true, message: '类型不能为空', trigger: 'blur' }
          ],
          imgUrl: [
            { required: true, message: '图片地址不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.url = this.$http.adornUrl(`/sys/oss/upload?token=${this.$cookie.get('token')}`)
        
        this.imageUrl=''
        this.dataForm.brandActivityId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.brandActivityId) {
            this.$http({
              url: this.$http.adornUrl(`/admin/brandactivity/info/${this.dataForm.brandActivityId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.type = data.brandActivity.type
                this.dataForm.imgUrl = data.brandActivity.imgUrl
                this.imageUrl=data.brandActivity.imgUrl
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/admin/brandactivity/${!this.dataForm.brandActivityId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'brandActivityId': this.dataForm.brandActivityId || undefined,
                'type': this.dataForm.type,
                'imgUrl': this.dataForm.imgUrl
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      handleAvatarSuccess( res, file ){
        this.imageUrl = URL.createObjectURL(file.raw);
        this.dataForm.imgUrl=res.url;
      },
      beforeAvatarUpload(file){
        if (file.type !== 'image/jpg' && file.type !== 'image/jpeg' && file.type !== 'image/png' && file.type !== 'image/gif') {
          this.$message.error('只支持jpg、png、gif格式的图片！')
          return false
        }
      }
    }
  }
</script>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>